package com.ziven.common.widget;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Point;
import android.graphics.Rect;
import android.os.Build;
import android.util.Log;
import android.util.TypedValue;
import android.view.WindowManager;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.annotation.StringRes;

import com.ziven.easygo.util.ResourceUtils;

/**
 * @author Ziven
 */
public final class WidgetUtils {

    private static final String DP = "dp";
    private static final String SP = "sp";
    private static final String XP = "xp";
    private static final int END_COUNT = 2;
    private static final String SP_NAME = "com_ziven_common_widget";
    private final static int[] SCREEN_SIZE = {0, 0};

    private WidgetUtils() {}

    public static SharedPreferences getSp() {
        return ResourceUtils.sp(SP_NAME);
    }

    public static ResourceUtils.Sp ofSp() {
        return ResourceUtils.ofSp(SP_NAME);
    }

    public static int dimenInt(@NonNull String dimen) {
        return (int) dimen(dimen);
    }

    public static float dimen(@NonNull String dimen) {
        if(dimen.endsWith(DP)) {
            return dp2px(dimen2Float(dimen, END_COUNT));
        }
        if(dimen.endsWith(SP)) {
            return sp2px(dimen2Float(dimen, END_COUNT));
        }
        if(dimen.endsWith(XP)) {
            return dimen2Float(dimen, END_COUNT);
        }
        return dimen2Float(dimen, 0);
    }

    private static float dimen2Float(@NonNull String dimen, int endCount) {
        try {
            return endCount == 0
                    ? Float.parseFloat(dimen)
                    : Float.parseFloat(dimen.substring(0, dimen.length() - endCount));
        } catch (Throwable th) {
            throw new RuntimeException(th);
        }
    }

    public static float dp2px(float dp) {
        return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp,
                ResourceUtils.getContext().getResources().getDisplayMetrics());
    }

    public static float sp2px(float sp) {
        return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, sp,
                ResourceUtils.getContext().getResources().getDisplayMetrics());
    }

    public static void showShort(@StringRes int textId) {
        showShort(ResourceUtils.getText(textId));
    }

    public static void showLong(@StringRes int textId) {
        showLong(ResourceUtils.getText(textId));
    }

    public static void showShort(@NonNull String text) {
        Toast.makeText(ResourceUtils.getContext(), text, Toast.LENGTH_SHORT).show();
    }

    public static void showLong(@NonNull String text) {
        Toast.makeText(ResourceUtils.getContext(), text, Toast.LENGTH_LONG).show();
    }

    @NonNull
    public static int[] getScreenSize() {
        if(SCREEN_SIZE[0] == 0 || SCREEN_SIZE[1] == 0) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
                getRealSizeFromMetrics(SCREEN_SIZE);
            } else {
                getRealSizeFromDisplay(SCREEN_SIZE);
            }
        }
        return new int[] {SCREEN_SIZE[0], SCREEN_SIZE[1]};
    }

    private static void getRealSizeFromDisplay(@NonNull int[] size) {
        try {
            Point point = new Point();
            getWindowManager().getDefaultDisplay().getRealSize(point);
            size[0] = point.x;
            size[1] = point.y;
        } catch (Throwable th) {
            Log.e("WidgetUtils", "getRealSizeFromDisplay", th);
        }
    }

    @RequiresApi(api = Build.VERSION_CODES.R)
    private static void getRealSizeFromMetrics(int[] size) {
        try {
            Rect rect = getWindowManager().getCurrentWindowMetrics().getBounds();
            size[0] = rect.width();
            size[1] = rect.height();
        } catch (Throwable th) {
            Log.e("WidgetUtils", "getRealSizeFromMetrics", th);
        }
    }

    private static WindowManager getWindowManager() {
        return (WindowManager) (ResourceUtils.getContext().getSystemService(Context.WINDOW_SERVICE));
    }
}
